package com.ruoyi.quartz.task.devicupdatetemperature;


import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yanjing.domain.DeviceTemperature;
import com.ruoyi.yanjing.domain.ElectricityPole;
import com.ruoyi.yanjing.domain.MonitorPoint;
import com.ruoyi.yanjing.domain.PointAlarmHistory;
import com.ruoyi.yanjing.service.IDeviceTemperatureService;
import com.ruoyi.yanjing.service.IElectricityPoleService;
import com.ruoyi.yanjing.service.IMonitorPointService;
import com.ruoyi.yanjing.service.IPointAlarmHistoryService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 定时任务-温度更新
 *
 * @author ruoyi
 */
@Component("autoUpdatePointAndDeviceJob")
public class AutoUpdatePointAndDeviceJob implements Runnable {

    private static final Log log = LogFactory.getLog(AutoUpdatePointAndDeviceJob.class);

    @Autowired
    IDeviceTemperatureService iDeviceTemperatureService;

    @Autowired
    IMonitorPointService iMonitorPointService;

    @Autowired
    IPointAlarmHistoryService iPointAlarmHistoryService;

    @Autowired
    IElectricityPoleService iElectricityPoleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized void run() {
        log.info("Auto update point and device launched.");
        List<DeviceTemperature> deviceTemperatureList = iDeviceTemperatureService.selectAllPointNewTemperatureList();
        List<Long> poleIds = deviceTemperatureList.stream().map(DeviceTemperature::getPoleId).distinct().collect(Collectors.toList());


        //批量更新电杆为正常，并将异常信息置空
        iElectricityPoleService.batchUpdateElectricityPoleByIds(poleIds);

        for (DeviceTemperature deviceTemperature : deviceTemperatureList) {
            String key = DictUtils.getDictLabel("temperature_limit","normal");
            if(StringUtils.isNotBlank(deviceTemperature.getCurrentTemperature()) && Double.valueOf(deviceTemperature.getCurrentTemperature() )> Double.valueOf(key)){
                //step1:更新设备的温度和电压
                if(deviceTemperature.getPointId() != null){
                    MonitorPoint monitorPoint = iMonitorPointService.selectMonitorPointById(deviceTemperature.getPointId().toString());
                    monitorPoint.setPointAlarmStatus("0");//异常
                    monitorPoint.setDeviceTemperature(deviceTemperature.getCurrentTemperature());
                    monitorPoint.setDeviceVoltage(deviceTemperature.getCurrentVoltage());
                    monitorPoint.setReportTime(deviceTemperature.getReportTime());
                    iMonitorPointService.updateMonitorPoint(monitorPoint);
                    //step1.1:更新电杆的信息
                    if(deviceTemperature.getPointId() != null){
                        String alarmInformation = monitorPoint.getPointName()+monitorPoint.getPointDeviceName()+monitorPoint.getPhase();
                        ElectricityPole electricityPole = iElectricityPoleService.selectElectricityPoleById(monitorPoint.getPoleId().toString());
                        electricityPole.setAlarmStatus("0");//异常
                        electricityPole.setAlarmInformation(StringUtils.isBlank(electricityPole.getAlarmInformation()) ?  alarmInformation : (electricityPole.getAlarmInformation()+"；"+alarmInformation));
                        iElectricityPoleService.updateElectricityPole(electricityPole);
                    }
                    //step2:插入温度异常表信息
                    PointAlarmHistory pointAlarmHistory = new PointAlarmHistory();
                    BeanUtils.copyProperties(monitorPoint, pointAlarmHistory);
                    iPointAlarmHistoryService.insertPointAlarmHistory(pointAlarmHistory);

                }
            }else{
                //step1:更新设备的温度和电压
                if(deviceTemperature.getPointId() != null){
                    MonitorPoint monitorPoint = iMonitorPointService.selectMonitorPointById(deviceTemperature.getPointId().toString());
                    monitorPoint.setPointAlarmStatus("1");//正常
                    monitorPoint.setDeviceTemperature(deviceTemperature.getCurrentTemperature());
                    monitorPoint.setDeviceVoltage(deviceTemperature.getCurrentVoltage());
                    monitorPoint.setReportTime(deviceTemperature.getReportTime());
                    iMonitorPointService.updateMonitorPoint(monitorPoint);
                }
            }
        }
    }

}
